using UnityEngine;
using System;
using System.Collections;

public class Dialog : MonoBehaviour
{
	public Texture2D[] dialogBoxes;
	public Transform dialogObject;
	public TextMesh dialogInScene;
	public Transform myDialogObject;
	public TextMesh characterName;
	public String dialogText;
	int countLetters = 0;
	
	public void CreateDialog (String text,Vector2 position,String nameDialog,CharacterMove move)
	{
		dialogInScene.text = "";	
		String[] textPartition = text.Split(' ');
		Debug.Log(dialogInScene.text);
		foreach(String newText in textPartition)
		{
			char[] myChar = newText.ToCharArray();
			countLetters += (myChar.Length + 1);
			if(countLetters < 25)
			{
				dialogInScene.text += newText + " ";
			}
			else if(countLetters >= 25)
			{
				dialogInScene.text += "\n" + newText + " ";
				countLetters = (myChar.Length + 1);
			}
		}

		characterName.text = "Nome: " + nameDialog;
		myDialogObject = Instantiate(dialogObject,new Vector3(position.x,position.y + 0.5f,-6),dialogObject.rotation) as Transform;
		myDialogObject.gameObject.name = "Nova conversa com: " + nameDialog; 
		move.dialogStart(myDialogObject.gameObject);
		countLetters = 0;
	}
}
